package com.anhry.app.safety.manage.zczb.struts.action;

import java.util.Date;
import java.util.List;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.manage.zczb.bean.TbZczbXwCheck;
import com.anhry.app.safety.manage.zczb.bean.TbZczbXwHzd;
import com.anhry.app.safety.manage.zczb.struts.base.ZczbBaseAction;
import com.anhry.app.safety.util.DateFormater;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.Page;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.UtilMethod;

/**
 * @date:2012-10-26
 * @remark:小微企业自查信息
 * @author:dongbin
 */

@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class XwAction extends ZczbBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private TbZczbXwCheck check;
	
	public String list(){
		int rowCount = 0;
		int num = (this.num != null && !"".equals(this.num ))?Integer.parseInt(this.num ):1;
		int size = (this.size != null && !"".equals(this.size ))?Integer.parseInt(this.size ):10;
		if(null!=this.getSearch() && "1".equals(this.getSearch())){num = 1;size = 10;}
		try {
			if("2".equals(this.cIsup)){//未上报的
				return this.unUpList();
			}
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String orderHql = "";//排序hql语句
			String hql = this.getQuaryHql(1);
			List objSize = this.utilService.getObjecList("TbZczbXwHzdView", hql,null,0,0);
			if(objSize != null && objSize.size() > 0){
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size,num,rowCount);
			this.setPage(page);
			this.setObjList(this.utilService.getObjecList("TbZczbXwHzdView", hql,orderHql,page.getStartRow(),page.getSize()));
			this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
			this.setTypeList(this.enterprisService.getTypeList(user));
			this.setHyList((List)this.contextPvd.getServletContextAttr(Util.HY_DEPT_LIST));
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("xw_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: xwAction:list()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 未上报企业列表
	 * @return
	 */
	public String unUpList(){
		int rowCount = 0;
		int num = (this.num != null && !"".equals(this.num ))?Integer.parseInt(this.num ):1;
		int size = (this.size != null && !"".equals(this.size ))?Integer.parseInt(this.size ):10;
		if(null!=this.getSearch() && "1".equals(this.getSearch())){num = 1;size = 10;}
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String orderHql = " order by t.countyId,t.unitId,t.deptId,t.id";//排序hql语句
			String hql = this.getQuaryHql(2);
			List objSize = this.utilService.getObjecList("TbCorpInfoView1", hql,null,0,0);
			if(objSize != null && objSize.size() > 0){
				rowCount = Integer.parseInt(objSize.get(0).toString());
			}
			Page page = new Page(size,num,rowCount);
			this.setPage(page);
			this.setObjList(this.utilService.getObjecList("TbCorpInfoView1", hql,orderHql,page.getStartRow(),page.getSize()));
			this.setSdList(this.enterprisService.getSdList((List)this.contextPvd.getServletContextAttr(Util.QU_DEPT_LIST),user));
			this.setTypeList(this.enterprisService.getTypeList(user));
			this.setHyList((List)this.contextPvd.getServletContextAttr(Util.HY_DEPT_LIST));
			this.setYearList(UtilMethod.getYearList(2012));
			this.setResult("xw_unup_list");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: xwAction:unUpList()异常："+e.getMessage());
		}
		return ERROR;
	}
	
	/**
	 * 删除企业上报记录
	 * @return
	 */
	public String hzdDele(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbXwHzd hzd = (TbZczbXwHzd)this.utilService.getObjectById(TbZczbXwHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					this.utilService.deleteBySql("delete tb_zczb_xw_yh yh where yh.bgd_id in(select bgd.id from tb_zczb_xw_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.utilService.deleteBySql("delete tb_zczb_xw_bgd bgd where bgd.hzd_id="+hzd.getId()+")");
					this.utilService.deleteBySql("delete tb_zczb_xw_check t where t.hzd_id="+hzd.getId()+")");
					this.utilService.delete(hzd);
					this.setTip("删除成功！");
					return this.list();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: xwAction:hzdDele()异常："+e.getMessage());
		}
		this.setTip("你要删除的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 查看报告单列表
	 * @return
	 */
	public String bgdList(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbXwBgd t where 1=1 and t.hzdId="+this.hzdId+" order by t.bgdNum"));
				this.initLook();
				this.setResult("xw_bgd_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: xwAction:bgdList()异常："+e.getMessage());
		}
		this.setTip("您要查看的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 记录查看部门信息
	 *
	 */
	public void initLook(){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		TbUserDept unit = null;//用户单位
		TbUserDept dept = null;//用户部门
		if(null!=user.getTbUserUnit()){
			unit = user.getTbUserUnit();
		}
		if(null!=user.getTbUserDept()){
			dept = user.getTbUserDept();
		}
		TbZczbXwCheck obj = null;
		try {
			if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
				TbZczbXwHzd hzd = (TbZczbXwHzd)this.utilService.getObjectById(TbZczbXwHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					obj = this.zczbService.getXwCheck(user,this.hzdId);
					if(null==obj){
						obj = new TbZczbXwCheck();
						obj.setCheckStatus("0");
						obj.setHzdId(hzd.getId());
						obj.setIsDele("0");
						obj.setUserId(user.getId());
						obj.setLookDate(new Date());
						if(null!=unit){
							obj.setUnitId(unit.getId());
							obj.setUnitName(unit.getDeptName());
						}
						if(null!=dept){
							obj.setDeptId(dept.getId());
							obj.setDeptName(dept.getDeptName());
						}
						this.utilService.saveOrUpdate(obj);
						hzd.setHzdLooks(hzd.getHzdLooks()+1);
						this.utilService.update(hzd);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("xwAction initLook()异常："+e.getMessage());
		}
	}
	
	/**
	 * 检测该用户是否已经查看了该企业上报信息
	 *
	 */
	public void hzdLookCheck(){
		this.contextPvd.getResponse().setContentType("charset=UTF-8");
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		try {
			if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
				if(null!=this.hzdId && !"".equals(this.hzdId)){
					TbZczbXwCheck obj = this.zczbService.getXwCheck(user, hzdId);
					if(null!=obj){
						this.contextPvd.getResponse().getWriter().write("1");
					}else{
						this.contextPvd.getResponse().getWriter().write("2");
					}
				}else{
					this.contextPvd.getResponse().getWriter().write("2");
				}
			}else{
				this.contextPvd.getResponse().getWriter().write("1");
			}
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczbAction hzdLookCheck() 异常："+e.getMessage());
		}
	}
	
	/**
	 * 跳转到自查核查页面
	 * @return
	 */
	public String hzdCheck(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbXwHzd hzd = (TbZczbXwHzd)this.utilService.getObjectById(TbZczbXwHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
						TbZczbXwCheck obj = this.zczbService.getXwCheck(user, hzdId);
						if(null!=obj){
							this.setCheck(obj);
						}
					}
					this.setResult("xw_hzd_check");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdBack()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 核查记录保存
	 * @return
	 */
	public String hzdCheckSave(){
		try {
			TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			if(null!=this.hzdId && this.hzdId.length()>0){
				TbZczbXwHzd hzd = (TbZczbXwHzd)this.utilService.getObjectById(TbZczbXwHzd.class, Long.parseLong(this.hzdId));
				if(null!=hzd){
					if(!"0".equals(grade) && !"1".equals(grade)){//不为管理员时记录查看数据
						TbZczbXwCheck obj = this.zczbService.getXwCheck(user, hzdId);
						if(null!=obj){
							if("0".equals(obj.getCheckStatus())){//如果为查看状态
								hzd.setHzdChecks(hzd.getHzdChecks()+1);
								this.utilService.update(hzd);
							}
							if(null!=this.checkDate && this.checkDate.length()>0){
								obj.setCheckDate(DateFormater.formatDate(this.checkDate));
							}else{
								obj.setCheckDate(new Date());
							}
							obj.setCheckFlag1(checkFlag1);
							obj.setCheckFlag2(checkFlag2);
							obj.setCheckFlag3(checkFlag3);
							obj.setCheckFlag4(checkFlag4);
							obj.setCheckTel(checkTel);
							obj.setCheckMsg(checkMsg);
							obj.setCheckPer(checkPer);
							obj.setCheckStatus("1");
							this.utilService.update(obj);
						}
					}
					this.setTip("填写成功！");
					return this.hzdCheck();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdBackSave()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 自查上报信息管理部门查看情况
	 * @return
	 */
	public String hzdLookShow(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbXwCheck t where 1=1 and t.hzdId="+this.hzdId+" order by t.id desc"));
				this.setResult("xw_look_show");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdLookShow()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 自查上报信息管理部门查看情况
	 * @return
	 */
	public String hzdCheckShow(){
		try {
			if(null!=this.hzdId && this.hzdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbXwCheck t where 1=1 and t.checkStatus=1 and t.hzdId="+this.hzdId+" order by t.checkDate desc,t.id desc"));
				this.setResult("xw_check_show");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("zczb: ZczbAction:hzdCheckShow()异常："+e.getMessage());
		}
		this.setTip("你要操作的数据不存在！");
		return ERROR;
	}
	
	/**
	 * 隐患列表
	 * @return
	 */
	public String yhList(){
		try {
			if(null!=this.bgdId && this.bgdId.length()>0){
				this.setObjList((List)this.utilService.getHqlQuery("from TbZczbXwYh t where 1=1 and t.bgdId="+this.bgdId+" order by t.yhType1,t.yhType2"));
				this.setResult("xw_yh_list");
				return SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("XwAction YhList() 异常："+e.getMessage());
		}
		this.setTip("数据不存在！");
		return ERROR;
	}
	
	public String getQuaryHql(int type){
		TbUser user = (TbUser)this.contextPvd.getSessionAttr(Util.SESSION_ADMINUSER);
		String grade = user.getTbUserGrade().getId().toString();//用户级别
		TbUserDept unit = null;//用户单位
		TbUserDept dept = null;//用户部门
		if(null!=user.getTbUserUnit()){
			unit = user.getTbUserUnit();
		}
		if(null!=user.getTbUserDept()){
			dept = user.getTbUserDept();
		}
		StringBuilder urls = new StringBuilder("");
		StringBuilder hql = new StringBuilder(" where 1=1 ");
		String key = "";
		if(null!=this.keyWord && this.keyWord.length()>0){
			if(null!=this.getSearch() && "1".equals(this.getSearch())){
				key = this.getKeyWord().trim();
			}else{
				key = this.contextPvd.decode(this.getKeyWord());	
			}
			this.setKeyWord(key);
			hql.append(" and t.corpName like '%"+key+"%'");
			urls.append("keyWord="+this.contextPvd.encode(this.keyWord));
		}else{
			urls.append("keyWord=");
		}
		
		if("3".equals(grade) && "3".equals(dept.getDeptClass())){//市科室行业部门
			hql.append(" and t.deptMainId="+dept.getId());
			urls.append("&cHy="+dept.getId().toString());
			this.setCHy(dept.getId().toString());
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else if("4".equals(grade) && "3".equals(dept.getDeptClass())){//县区科室行业部门
			if((null==this.cMaxType || "".equals(this.cMaxType)) && (null==this.cMinType || "".equals(this.cMinType))){
				hql.append(" and t.quDeptIds like '%"+dept.getId()+"%'");
			}else{
				if(null!=this.cMaxType && this.cMaxType.length()>0){
					hql.append(" and t.maxTypeId="+this.cMaxType);
					urls.append("&cMaxType="+this.cMaxType);
				}
				if(null!=this.cMinType && this.cMinType.length()>0){
					hql.append(" and t.minTypeId="+this.cMinType);
					urls.append("&cMinType="+this.cMinType);
				}
			}
			if(null!=this.cHy && this.cHy.length()>0){
				hql.append(" and t.deptMainId="+this.cHy);
				urls.append("&cHy="+this.cHy);
			}
		}else if("5".equals(grade)){//市行业
			if("1".equals(unit.getDeptClass())){//行业监管
				hql.append(" and t.deptMainId="+unit.getId());
				urls.append("&cHy="+unit.getId().toString());
				this.setCHy(unit.getId().toString());
			}else{//专业监管
				if("5310".equals(unit.getId().toString())){//质监局
					hql.append(" and t.corpIsTeshe=1");
				}else if("5311".equals(unit.getId().toString())){//消防局
					
				}else if("5312".equals(unit.getId().toString())){//气象局
					hql.append(" and t.corpIsBlzh=1");
				}
				if(null!=this.cHy && this.cHy.length()>0){
					hql.append(" and t.deptMainId="+this.cHy);
					urls.append("&cHy="+this.cHy);
				}
			}
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else if("6".equals(grade)){//区行业
			if("1".equals(dept.getDeptClass())){
				hql.append(" and t.deptMainId="+dept.getDeptSj());
				urls.append("&cHy="+dept.getDeptSj().toString());
				this.setCHy(dept.getDeptSj().toString());
			}else{
				if("5310".equals(dept.getDeptSj().toString())){//质监局
					hql.append(" and t.corpIsTeshe=1");
				}else if("5311".equals(dept.getDeptSj().toString())){//消防局
					
				}else if("5312".equals(dept.getDeptSj().toString())){//气象局
					hql.append(" and t.corpIsBlzh=1");
				}
				if(null!=this.cHy && this.cHy.length()>0){
					hql.append(" and t.deptMainId="+this.cHy);
					urls.append("&cHy="+this.cHy);
				}
			}
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
		}else{//其他用户
			if(null!=this.cMaxType && this.cMaxType.length()>0){
				hql.append(" and t.maxTypeId="+this.cMaxType);
				urls.append("&cMaxType="+this.cMaxType);
			}
			if(null!=this.cMinType && this.cMinType.length()>0){
				hql.append(" and t.minTypeId="+this.cMinType);
				urls.append("&cMinType="+this.cMinType);
			}
			if(null!=this.cHy && this.cHy.length()>0){
				hql.append(" and t.deptMainId="+this.cHy);
				urls.append("&cHy="+this.cHy);
			}
		}
		
		if("4".equals(grade) || "6".equals(grade) || "8".equals(grade)){//县区安委会、区科室、区行业
			hql.append(" and t.countyId="+unit.getId());
			this.setCCounty(unit.getId().toString());
			if(null!=this.cUnit && this.cUnit.length()>0){
				hql.append(" and t.unitId="+this.cUnit);
				urls.append("&cUnit="+this.cUnit);
			}
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}else if("7".equals(grade)){//属地
			hql.append(" and t.countyId="+unit.getId()+" and t.unitId="+dept.getId());
			this.setCCounty(unit.getId().toString());
			this.setCUnit(dept.getId().toString());
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}else{
			if(null!=this.cCounty && this.cCounty.length()>0){
				hql.append(" and t.countyId="+this.cCounty);
				urls.append("&cCounty="+this.cCounty);
			}
			if(null!=this.cUnit && this.cUnit.length()>0){
				hql.append(" and t.unitId="+this.cUnit);
				urls.append("&cUnit="+this.cUnit);
			}
			if(null!=this.cDept && this.cDept.length()>0){
				hql.append(" and t.deptId="+this.cDept);
				urls.append("&cDept="+this.cDept);
			}
		}
		if(null!=this.cZcType && this.cZcType.length()>0){
			hql.append(" and t.corpReportType="+this.cZcType);
			urls.append("&cZcType="+this.cZcType);
		}
		if(null!=this.cGrade && this.cGrade.length()>0){
			if("E".equals(this.cGrade)){
				hql.append(" and t.corpGrade is null");
			}else{
				hql.append(" and t.corpGrade='"+this.cGrade+"'");
			}
			urls.append("&cGrade="+this.cGrade);
		}
		if(null==this.cYear || "".equals(this.cYear)){
			this.setCYear(UtilMethod.getZcYear());
		}
		if(null==this.cMonth || "".equals(this.cMonth)){
			this.setCMonth(UtilMethod.getZclMonth());
		}
		if(type==1){
			hql.append(" and t.hzdStatus=1");
			hql.append(" and t.hzdYear="+this.cYear);
			urls.append("&cYear="+this.cYear);
			hql.append(" and t.hzdMonth="+this.cMonth);
			urls.append("&cMonth="+this.cMonth);
			
			if(null!=this.cHecha && this.cHecha.length()>0){
				if("1".equals(this.cHecha)){
					hql.append(" and t.hzdLooks=0");
				}else if("2".equals(this.cHecha)){
					hql.append(" and t.hzdChecks>0");
				}else if("3".equals(this.cHecha)){
					hql.append(" and t.hzdLooks=0");
				}else if("4".equals(this.cHecha)){
					hql.append(" and t.hzdChecks>0");
				}
				urls.append("&cHecha="+this.cHecha);
			}
			if(null!=this.cYh && this.cYh.length()>0){
				if("1".equals(this.cYh)){
					hql.append(" and t.hzdYhNum=0");
				}else if("2".equals(this.cYh)){
					hql.append(" and t.hzdYhNum>0");
				}else if("3".equals(this.cYh)){
					hql.append(" and t.hzdWclNum>0");
				}
				urls.append("&cYh="+this.cYh);
			}
			if(null==this.cIsup || "".equals(this.cIsup)){
				this.setCIsup("1");
			}
			urls.append("&cIsup="+this.cIsup);
		}else{
			hql.append(" and not exists(from TbZczbHzd x where x.corpId=t.id and x.hzdYear="+this.cYear+" and x.hzdMonth="+this.cMonth+" and x.hzdStatus=1)");
			hql.append(" and t.corpReportType=2");
			hql.append(" and ((t.corpAddTime <= to_date('").append(UtilMethod.getZcEdate(Integer.parseInt(this.cYear), Integer.parseInt(this.cMonth))+"','yyyy-MM-dd') and t.isDele=0) ")
			.append(" or (t.corpAddTime <= to_date('").append(UtilMethod.getZcEdate(Integer.parseInt(this.cYear), Integer.parseInt(this.cMonth))+"','yyyy-MM-dd') and t.updateTime >= to_date('")
			.append(UtilMethod.getZcEdate(Integer.parseInt(this.cYear), Integer.parseInt(this.cMonth))+"','yyyy-MM-dd') and t.isDele=1)) ");
		}
		this.setUrls(urls.toString());
		return hql.toString();
	}

	public TbZczbXwCheck getCheck() {
		return check;
	}

	public void setCheck(TbZczbXwCheck check) {
		this.check = check;
	}


}
